﻿using IDAL.SH_ADF0979IDAL;

using Microsoft.EntityFrameworkCore;
using MysqlforDataWatch;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tools;
using Tools.AddDistance;
using Tools.ErrorMessage;
using Tools.FileOperation;
using Tools.ListOperation;
using Tools.ListOperation.BrakeListOperation;
using Tools.ListOperation.BumpListOperation;
using Tools.MyConfig;
using static Tools.FileOperation.CSVFileImport;

namespace DAL.SH_ADF0979DAL
{
    public class BumpDistribution_DAL : BaseDAL<Bumprecognition>, IBumpDistribution_IDAL
    {
        public BumpDistribution_DAL(datawatchContext DB) : base(DB)
        {

        }

        public string JudgeandMergeBmpDataperHalfHourV2(FileInfo file, CSVStatisticDataAllImport importstruct, ref List<Bumprecognition> blist, string vehicleid, in VehicleIDPara vehicleIDPara)
        {
            try
            {
                var time = Convert.ToDateTime(file.Name.Split('-')[0].Replace("_", "-"));
                if (vehicleIDPara.BumpImport == 1)
                {
                    var sqllist = _DB.Bumprecognitions.Where(a => a.Datadate > time && a.Datadate < time.AddDays(1) && a.VehicleId == vehicleid).Select(a => a.VehicleId).AsNoTracking().FirstOrDefault();
                    if (string.IsNullOrEmpty(sqllist))
                    {
                        if (importstruct.Iscontinue)
                        {
                            var acczlf = importstruct.headers.ToList().IndexOf(vehicleIDPara.acczwhllf);
                            var acczrf = importstruct.headers.ToList().IndexOf(vehicleIDPara.acczwhlrf);
                            var acczlr = importstruct.headers.ToList().IndexOf(vehicleIDPara.acczwhllr);
                            var spd = importstruct.headers.ToList().IndexOf(vehicleIDPara.speedcolumnname);

                            if (acczlf != -1 && acczrf != -1 && acczlr != -1)
                            {
                                var list = AddBump.addbumplist(importstruct.lists[acczlf], importstruct.lists[acczrf], importstruct.lists[acczlr], importstruct.lists[spd], vehicleid, importstruct.name, importstruct.datetime, in vehicleIDPara);
                                if (list.Count > 0)
                                {
                                    blist = blist.Concat(list).ToList();
                                }
                                return null;
                            }
                            else
                            {
                                return ($"此文件{file.Name}冲击计算时出现了问题，通道{vehicleIDPara.acczwhllf}或{vehicleIDPara.acczwhlrf}或{vehicleIDPara.acczwhllr}或{vehicleIDPara.speedcolumnname}没有找到！{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                            }
                        }
                        else
                        {
                            return ($"此文件{file.Name}冲击计算时出现了问题，数据状态为{importstruct.status}！{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                        }
                    }
                    else
                    {
                        return ($"此文件{file.Name}冲击计算时已导入过，须删除后再重新导入！{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");

                    }
                }

                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                errormessage.getErrormessage(file, ex.Message);

                throw;
            }

        }

    }
}
